<template>
  <el-dialog title="设置角色" :visible.sync="dialogShow" @close="closeDialog">
    <!-- //!多选框 -->
    <el-checkbox-group v-model="checkList">
      <!-- v-model是用来收集和回显对应数据的，和label字段的数据是关联的 -->
      <!-- 默认我们显示的也是label的内容，除非我们使用了插值表达式 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row type="flex" justify="end">
      <el-button size="small" @click="dialogShow = false">取消</el-button>
      <el-button size="small" type="primary" @click="setRoleSubmit">
        确认
      </el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { assignRoles, getEmployeesInfoApi } from '@/api/employees'
export default {
  name: 'SetRole',
  data() {
    return {
      dialogShow: false,
      roleList: [], // 所有角色
      checkList: [], // 勾选中的
      userId: null // 当前员工Id
    }
  },
  methods: {
    async getAllRoleList() {
      const { rows } = await getRoleListApi({
        page: 1,
        pagesize: 10000
      })
      this.roleList = rows
    },
    // 获取当前员工的信息
    async getCurrentRole(id) {
      const res = await getEmployeesInfoApi(id)
      this.userId = id
      this.checkList = res.roleIds || []
    },
    // 设置角色信息
    async setRoleSubmit() {
      // 发送请求
      await assignRoles({
        id: this.userId, // 当前员工的Id
        roleIds: this.checkList // 选中的角色ID
      })
      // 提示成功
      this.$message.success('关联成功')
      // 弹框关闭
      this.dialogShow = false
    },
    // 弹窗关闭释放操作
    closeDialog() {
      this.checkList = []
      this.userId = null
    }
  }
}
</script>

<style></style>
